package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrdersettingService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ordersetting")
public class OrdersettingController {
    @Reference
    private OrdersettingService ordersettingService;
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile file){
        try {
            List<String[]> strings = POIUtils.readExcel(file);//返回一个excel表的数据，string[]代表一行，数组中的每一个元素代表单元格中的数据
            List<OrderSetting> setting = new ArrayList<>();
            //将表中数据取出，并进行封装没然后存到数组库
            for (String[] string : strings) {
                OrderSetting orderSetting = new OrderSetting(new Date(string[0]), Integer.parseInt(string[1]));
                setting.add(orderSetting);
            }
            ordersettingService.add(setting);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        }

    }
    @RequestMapping("/find")
    public Result find(String date){

        if(date!=null&&date.length()>0){

            List<Map<String, Object>> list= ordersettingService.find(date);

            return new Result(true,null,list);
        }
        return new Result(true,null);
    }
    @RequestMapping("/ediNumberByDate")
    public Result ediNumberByDate(@RequestBody OrderSetting orderSetting){

        try {
            ordersettingService.ediNumberByDate(orderSetting);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
        return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
    }

}
